package com.loooooo.pm2024.utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;

import java.io.IOException;
import java.net.InetAddress;


public class ScpUtils {

    public static boolean localToServer(String userName, String password, String ipAddr, String loadFilePath, String serverPath) {
        boolean isAuthed = false;
        try {
            if (InetAddress.getByName(ipAddr).isReachable(1500)) {
                Connection conn = new Connection(ipAddr);
                conn.connect();
                isAuthed = conn.authenticateWithPassword(userName, password);
                if (isAuthed) {
                    SCPClient scpClient = conn.createSCPClient();

                    scpClient.put(loadFilePath, serverPath);
                    conn.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return isAuthed;
    }

    public static boolean localToServer(String userName, String password, String ipAddr,  String[] loadFilePath, String serverPath) {
        boolean isAuthed = false;

        try {
            if (InetAddress.getByName(ipAddr).isReachable(1500)) {
                Connection conn = new Connection(ipAddr);
                conn.connect();
                isAuthed = conn.authenticateWithPassword(userName, password);
                if (isAuthed) {
                    SCPClient scpClient = conn.createSCPClient();

                    scpClient.put(loadFilePath , serverPath,"0600");
                    conn.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return isAuthed;
    }

    public static boolean serverToLocal(String userName, String password, String ipAddr, String serverPath, String localPath) {
        boolean isAuthed = false;
        boolean status = false;
        try {
            System.out.println(ipAddr);
            status = InetAddress.getByName(ipAddr).isReachable(1500);
            System.out.println(status);
            if (status) {
                Connection conn = new Connection(ipAddr);
                conn.connect();
                isAuthed = conn.authenticateWithPassword(userName, password);
                System.out.println(isAuthed);
                if (isAuthed) {
                    Session session = conn.openSession();
                    SCPClient scpClient = conn.createSCPClient();
                    scpClient.get(serverPath, localPath);
                    session.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return isAuthed;
    }
    public static void main(String[] args) {
        // localToServer("tech","zxcvb","192.168.10.2","D://Tools//test.txt","/home/tech/test/");
        serverToLocal("tech", "zxcvb", "192.168.10.2", "/home/tech/test/***", "D://Tools//test//");
    }
}
